package com.vancouver.knows.ui.fragment.homePage;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.bean.UserPointsDetailsInfo;
import com.vancouver.knows.util.CircleTransform;
import com.vancouver.knows.business.BSharedPrefManager;

import java.util.List;

public class HomepageAfterLoginView {
	private Context mContext;
	private LayoutInflater mInflater;
	private View mView;
	@Bind(R.id.comeback_tips1)
	TextView textInfo;
	@Bind(R.id.comeback_tips)
	TextView comebackTips;
	@Bind(R.id.img_user)
	ImageView userImage;

	private BSharedPrefManager sharedPreManager;

	public HomepageAfterLoginView(Context context, LayoutInflater inflater) {
		mContext = context;
		mInflater = inflater;
		findViewsId();
		setViews();
	}

	private void findViewsId() {
		mView = mInflater.inflate(R.layout.login_success, null);
		ButterKnife.bind(this, mView);
	}

	public void setViews() {
		sharedPreManager = ((MainApplication) mContext.getApplicationContext()).getSharedPrefManager();
		textInfo.getPaint().setFakeBoldText(true);// 加粗

		UserInfo userInfo = ((MainApplication) mContext.getApplicationContext()).getBusinessModule()
				.getCacheManager().getUserInfo();
		if (userInfo != null) {
			textInfo.setText(Html.fromHtml(mContext.getString(R.string.homepage_comeback_tips1)
					+ "<font color=\"#ff0000\">" + sharedPreManager.getUserName() + "</font>"));
			if (userInfo.userPointsDetailsInfo != null && userInfo.userPointsDetailsInfo.getTotalPoints() != null) {
				comebackTips.setText(Html.fromHtml(mContext.getString(R.string.home_exist_points, userInfo.userPointsDetailsInfo.getTotalPoints())));
			}else {
				comebackTips.setText(mContext.getString(R.string.homepage_comeback_tips2));
			}

			UserInfo.UserMeta userMeta = sharedPreManager.getUserMeta();
			String avatarUrl;
			if (userMeta != null) {
				avatarUrl = userMeta.getAvatar();
			} else {
				avatarUrl = sharedPreManager.getAvatar();
			}
			setUserImg(avatarUrl);
		}

	}

	private void setUserImg(String imgUrl) {
		if (StringUtil.isEmptyString(imgUrl)) {
			Picasso.with(mContext).load(R.drawable.me_icon_login).transform(new CircleTransform()).centerCrop()
					.resizeDimen(R.dimen.detail_user_followed_size, R.dimen.detail_user_followed_size).into(userImage);
		} else {
			Picasso.with(mContext).load(imgUrl).transform(new CircleTransform()).centerCrop()
					.resizeDimen(R.dimen.detail_user_followed_size, R.dimen.detail_user_followed_size).into(userImage);
		}
	}

	public View getAfterLoginView() {
		return mView;
	}
}